<?php
if(!session_id()) { session_start(); }
if(!isset($_SESSION['user_id'])) {
	header('Location: index.php');
}
include_once('functions/main_functions.php');
include_once('classes/portfolio.class.php');
	page_head();
?>
		<div id="main">
			<h2>Option Calculator</h2>
			<h3>Binomial Portfolio</h3>
			<table id="portfolio-table">
				<tr>
					<th>
						Date
					</th>
					<th>
						Time
					</th>
					<th>
						Type
					</th>
					<th>
						Stock Name
					</th>
					<th>
						Price
					</th>
					<th>
						Option Price
					</th>
					<th>
						Expiry
					</th>
					<th>
						Interest Rate
					</th>
					<th>
						Volatility
					</th>
					<th>
						Calculated
					</th>
					<th>
						Actions
					</th>
				</tr>
				<?php
				$time_zone = "Asia/Singapore";
				$date_time = new DateTime();
				$date_time_zone = new DateTimeZone($time_zone);
				$date_time->setTimezone($date_time_zone);
				$portfolio = new Portfolio();
				$results = $portfolio->retrieveAll();
				if($results != 0) {
					for($i=0; $i < count($results); $i++) {
						$date_time->setTimestamp($results[$i]['portfolio_unix_time']);
						echo '
						<tr>
							<td>
								';
							echo $date_time->format("y/m/d");
						echo '
						</td>
						<td>
						';
						echo $date_time->format("h:m:s");
						echo'
						</td>
							<td>
								';
							if($results[$i]['portfolio_type'] == 0) {
								echo 'Call';
							} else {
								echo 'Put';
							}
						echo '
							</td>
							<td>
								' . $results[$i]['portfolio_name'] . '
							</td>
							<td>
								$' . $results[$i]['portfolio_price'] . '
							</td>
							<td>
								$' . $results[$i]['portfolio_option_price'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_time_expire'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_interest_rate'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_volatility'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_result'] . '
							</td>
							<td id="portfolio-actions">
								<a id="portfolio-delete-button" onclick="delete_portfolio(' . $results[$i]['portfolio_id'] . '); return false;">Delete</a>
							</td>
						</tr>
						';
					}
				} else {
					echo '<tr><td colspan="10">Your portfolio is empty. Click <a href="calculator.php">here</a> to begin adding some if calculator is available for Binomial.</td></tr>';
				}
				?>
			</table>
			
			<h3>Black Scholes Portfolio</h3>
			<table id="portfolio-table">
				<tr>
					<th>
						Date
					</th>
					<th>
						Time
					</th>
					<th>
						Type
					</th>
					<th>
						Stock Name
					</th>
					<th>
						Price
					</th>
					<th>
						Option Price
					</th>
					<th>
						Expiry
					</th>
					<th>
						Interest Rate
					</th>
					<th>
						Volatility
					</th>
					<th>
						Calculated
					</th>
					<th>
						Actions
					</th>
				</tr>
				<?php
				$time_zone = "Asia/Singapore";
				$date_time = new DateTime();
				$date_time_zone = new DateTimeZone($time_zone);
				$date_time->setTimezone($date_time_zone);
				$portfolio = new Portfolio();
				$results = $portfolio->retrieveAllBlack();
				if($results != 0) {
					for($i=0; $i < count($results); $i++) {
						$date_time->setTimestamp($results[$i]['portfolio_unix_time']);
						echo '
						<tr>
							<td>
								';
							echo $date_time->format("y/m/d");
						echo '
						</td>
						<td>
						';
						echo $date_time->format("h:m:s");
						echo'
						</td>
							<td>
								';
							if($results[$i]['portfolio_type'] == 0) {
								echo 'Call';
							} else {
								echo 'Put';
							}
						echo '
							</td>
							<td>
								' . $results[$i]['portfolio_name'] . '
							</td>
							<td>
								$' . $results[$i]['portfolio_price'] . '
							</td>
							<td>
								$' . $results[$i]['portfolio_option_price'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_time_expire'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_interest_rate'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_volatility'] . '
							</td>
							<td>
								' . $results[$i]['portfolio_result'] . '
							</td>
							<td id="portfolio-actions">
								<a id="portfolio-delete-button" onclick="delete_portfolio_black(' . $results[$i]['portfolio_id'] . '); return false;">Delete</a>
							</td>
						</tr>
						';
					}
				} else {
					echo '<tr><td colspan="10">Your portfolio is empty. Click <a href="calculator.php">here</a> to begin adding some if calculator is available for Black Scholes.</td></tr>';
				}
				?>
			</table>
		</div>
<?php
	page_foot();
?>